#include<stdio.h>
typedef struct hjx
{
    int *a;
    int daxiao;
    int neicun;
}hjx;
void jiaohuan(int *fumu,int *child)
{
    int t;
    t=*fumu;
    *fumu=*child;
    *child=t;
}
void ajust(int*a,int daxiao,int root)
{
    int fumu=root;
    int child=fumu*2+1;
    while(child<daxiao)
    {
        if(child+1<daxiao&&a[child]>a[child+1])
    {
        child++;
    }
    if(a[fumu]>a[child])
    {
        jiaohuan(&a[fumu],&a[child]);
        fumu=child;
        child=2*fumu+1;

    }
    else
    {
        break;
    }

    }
}
void chushihua(hjx*p,int *a,int sz)
{
    p->a=(int*)malloc(sizeof(int)*sz);
    memcpy(p->a,a,sizeof(int )*sz);
    p->daxiao=sz;
    p->neicun=sz;
    int i;
    for(i=(sz-1-1-1)/2;i>=0;i--)
    {
        ajust(p->a,p->daxiao,i);
    }
}
void print(int *a,int sz)
{
    int i=0;
    for(i=0;i<sz;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
}
void jiangxu(hjx*p)
{
    int end=p->daxiao-1;
    while(end>0)
    {
      jiaohuan(&(p->a[0]),&(p->a[end]));
      ajust(p->a,end,0);
      end--;
    }
}
void ajustup(int *a,int daxiao,int child)
{
    int fumu=(child-1)/2;
    int child1=child;
    while(child1>0)
    {
        if(a[fumu]>a[child1])
        {
            jiaohuan(&a[fumu],&a[child1]);
            child1=fumu;
            fumu=(child1-1)/2;
        }
        else
        {
            break;
        }
    }
}
void charu(hjx*p)
{
    int x;
    scanf("%d",&x);
    if(p->daxiao==p->neicun)
    {
        p->neicun=(p->neicun)*2;
        p->a=(int *)realloc(p->a,sizeof(int )*p->neicun);

    }
    p->a[p->daxiao]=x;
    p->daxiao++;
    ajustup(p->a,p->daxiao,p->daxiao-1);
}
int main()
{
    hjx p;
    int a[]={27,15,19,18,28,34,65,49,25,37};
    int sz=sizeof(a)/sizeof(a[0]);
    chushihua(&p,a,sz);
    print(p.a,sz);
//    jiangxu(&p);
    charu(&p);
     print(p.a,sz);
    return 0;
}
